<!--
 * @Author: 李天惊 uf_lee@163.com
 * @Date: 2022-08-15 16:45:26
 * @LastEditors: 李小晴 2689279066@qq.com
 * @LastEditTime: 2023-10-29 10:56:45
 * @FilePath: \grazlink20\uc_sys\src\views\Asset\Pasture\components\See.vue
 * Copyright (c) 2022 by 李天惊 uf_lee@163.com, All Rights Reserved. 
-->
<script setup lang="ts">
import { onBeforeMount, PropType, reactive, ref } from 'vue'
import type { TableData } from '@/api/uc/org/types'
import { Descriptions } from '@/components/Descriptions'

import { ContentWrap } from '@/components/ContentWrap'
import { Table } from '@/components/Table'

// 获取字典
import { useCache } from '@/hooks/web/useCache'
import { zcLoanOrderDetailedListApi, zcLoanOrderSeeApi } from '@/api/cy/Loanapply'
import { sysRelFileListApi } from '@/api/fa/insurance'
// import { zcAssetSelectAdminApi } from '@/api/fa/earcon'
const { wsCache } = useCache()
const dict: any = wsCache.get('dict')
// 字典通过value获取label
const getDictLabel = (type, value) => {
  let data = dict[type].find((item) => {
    return item.value == value
  })
  return data ? data.label : ''
}

const props = defineProps({
  currentRow: {
    type: Object as PropType<Nullable<TableData>>,
    default: () => null
  },
  detailSchema: {
    type: Array as PropType<DescriptionsSchema[]>,
    default: () => []
  },
  id: {
    type: String,
    default: null
  }
})
console.log(props)
const data = ref<any>({})
onBeforeMount(async () => {
  const res: any = await zcLoanOrderSeeApi({ id: props.id })
    .catch(() => {})
    .finally(() => {})
  if (res) {
    let aa = JSON.parse(res.data.select_json)
    const ming: any = await zcLoanOrderDetailedListApi({
      loan_order_id: props.id,
      my_org_id: wsCache.get('userInfo').default_org_id,
      inventory_type: '',
      is_inventory: '0',
      pageNo: 1,
      pageSize: 1100
    })
      .catch(() => {})
      .finally(() => {})
    if (ming) {
      console.log(res.data, aa, ming.data, '11111')
      detailedData.value = await ming.data
      data.value = res.data

      // 明细信息处理一下，加上d_id
      // data.value = await changeDId(res.data)
      // // 明细信息处理一下，加上d_id
      // await methods.setValues(data.value)
      // data.value.selected = res.data
      console.log(props.id, await ming.data, '11111')
    }

    const ress = await sysRelFileListApi({
      source_id: props.id,
      source: 'loan_order',
      pageNo: 1,
      pageSize: 20
    })
      .catch(() => {})
      .finally(() => {})
    if (ress) {
      console.log(ress.data)
      data.value.fileData = ress.data
      data.value.fileData.forEach((element) => {
        element.url = element.file_url
      })
    }
  }
})

const detailedData = ref()

const d_columns = reactive<TableColumn[]>([
  {
    field: 'index',
    label: '主键',
    type: 'index',
    width: 30
  },
  {
    field: 'asset_code',
    label: '注射编码',
    width: 200
  },
  {
    field: 'asset_code_2',
    label: '项圈编码',
    width: 200
  },
  {
    field: 'earcon_code',
    width: 200,
    label: '耳标编码'
  },
  {
    field: 'breed',
    label: '畜别',
    formatter: (_: Recordable, __: TableColumn, cellValue: number | string) => {
      return getDictLabel('breed', cellValue)
    },
    width: 100,
    search: {
      show: true
    }
  },
  {
    field: 'breed_sub',
    label: '品种',
    width: 200,
    formatter: (_: Recordable, __: TableColumn, cellValue: number | string) => {
      return getDictLabel('breed_sub', cellValue)
    }
  },
  {
    field: 'loan_state',
    label: '贷款状态',
    width: 150,
    formatter: (_: Recordable, __: TableColumn, cellValue: number | string) => {
      return getDictLabel('loan_order_state', cellValue)
    }
  }
])

const l_schema: any = [
  {
    field: 'bank_org_name',
    label: '贷款组织',
    component: 'DialogRefer'
  },
  {
    field: 'bank_dept_name',
    label: '贷款部门',
    component: 'TreeSelect'
  },
  {
    field: 'bank_user_name',
    label: '负责人',
    component: 'TreeSelect'
  },
  // { component: 'InputNumber', field: 'number', label: '牲畜数量' },
  {
    component: 'DatePicker',
    field: 'loan_data',
    label: '贷款日期',
    componentProps: {
      valueFormat: 'YYYY-MM-DD'
    }
  },
  {
    field: 'loan_year',
    label: '贷款年限',
    colProps: {
      span: 10
    }
  },
  {
    field: 'loan_no',
    label: '贷款单号',
    colProps: {
      span: 12
    }
  },
  {
    field: 'contract_note',
    label: '备注',
    colProps: {
      span: 12
    }
  }
]

const file_columns = reactive<TableColumn[]>([
  {
    field: 'index',
    label: '序号',
    type: 'index',
    width: 30
  },
  {
    field: 'file_url',
    label: '文件'
  },
  {
    field: 'file_size',
    label: '文件大小',
    table: { show: true },
    search: { show: false },
    form: { show: false },
    detail: { show: true }
  },
  {
    field: 'create_user_name',
    label: '创建人',
    width: 150,
    table: { show: true },
    search: { show: false },
    form: { show: true },
    detail: { show: false }
  }
])

const dblclick = async (a) => {
  const url = a.file_url // 打开的地址
  window.open(url)
}
</script>

<template>
  <ContentWrap title="主数据">
    <Descriptions :schema="props.detailSchema" :data="props.currentRow">
      <template #breed="{ row }: { row: any }">
        <div>{{ getDictLabel('breed', row.breed) }}</div>
      </template>
      <template #breed_sub="{ row }: { row: any }">
        <div>{{ getDictLabel('breed_sub', row.breed_sub) }}</div>
      </template>
      <template #type="{ row }: { row: any }">
        <div>{{ getDictLabel('loan_order_type', row.type) }}</div>
      </template>
      <template #state="{ row }: { row: any }">
        <div>{{ getDictLabel('loan_order_state', row.state) }}</div>
      </template>
    </Descriptions>
  </ContentWrap>

  <ContentWrap title="明细数据">
    <div class="mb-10px"> </div>
    <Table :columns="d_columns" :data="detailedData" :selection="false" :border="true">
      <template #earcon_id="{ row }">
        <div>{{ row.earcon_id }}</div>
      </template>

      <template #medical_date="{ row }">
        <div>{{ row.medical_date }}</div>
      </template>

      <template #symptom="{ row }">
        <div>{{ row.symptom }}</div>
      </template>

      <template #state="{ row }">
        <div>{{ row.state }}</div>
      </template>

      <template #medical_user_id="{ row }">
        <div>{{ row.medical_user_id }}</div>
      </template>
    </Table>
  </ContentWrap>

  <ContentWrap title="贷款合同信息" v-if="props.currentRow?.state == 3">
    <Descriptions :schema="l_schema" :data="data">
      <template #breed="{ row }: { row: any }">
        <div>{{ getDictLabel('breed', row.breed) }}</div>
      </template>
      <template #breed_sub="{ row }: { row: any }">
        <div>{{ getDictLabel('breed_sub', row.breed_sub) }}</div>
      </template>
      <template #type="{ row }: { row: any }">
        <div>{{ getDictLabel('quarantine_order_type', row.type) }}</div>
      </template>
      <template #state="{ row }: { row: any }">
        <div>{{ getDictLabel('loan_order_state', row.state) }}</div>
      </template>
    </Descriptions>
  </ContentWrap>

  <ContentWrap title="贷款合同" v-if="props.currentRow?.state == 3">
    <!-- <Table :columns="file_columns" :data="data.fileData" :selection="false" :border="true">
      <template #file_url="{ row }">
        <img :src="row.file_url" alt="" />
      </template>
    </Table> -->
    <Table
      :columns="file_columns"
      :data="data.fileData"
      :selection="false"
      @row-dblclick="dblclick"
    >
      <template #file_url="{ row }">
        <div>
          <ElImage
            v-if="row.file_type == 1 || row.mime_type == 'image/jpeg'"
            style="width: 40px; height: 40px; border-radius: 10px"
            :src="row.file_url"
          />
          <ElImage
            v-if="row.suffix_name == 'doc' || row.suffix_name == 'docx'"
            style="width: 40px; height: 40px; border-radius: 10px"
            src="https://docs.gtimg.com/docs-design-resources/document-management/tencent-docs/png@3x/application-vnd.tdocs-apps.doc@3x.png"
          />
          <ElImage
            v-if="row.suffix_name == 'xls' || row.suffix_name == 'xlsx'"
            style="width: 40px; height: 40px; border-radius: 10px"
            src="https://docs.gtimg.com/docs-design-resources/document-management/tencent-docs/png@3x/application-vnd.tdocs-apps.sheet@3x.png"
          />
          <ElImage
            v-if="row.suffix_name == 'ppt'"
            style="width: 40px; height: 40px; border-radius: 10px"
            src="https://docs.gtimg.com/docs-design-resources/document-management/tencent-docs/png-200px/application-vnd.tdocs-apps.slide.png"
          />
          <ElImage
            v-if="row.suffix_name == 'file'"
            style="width: 40px; height: 40px; border-radius: 10px"
            src="https://docs.gtimg.com/docs-design-resources/document-management/tencent-docs/png-200px/application-vnd.tdocs-apps.folder.png"
          />
          <ElImage
            v-if="row.suffix_name == 'txt'"
            style="width: 40px; height: 40px; border-radius: 10px"
            src="https://img1.baidu.com/it/u=4162092432,2125414077&fm=253&fmt=auto&app=138&f=JPEG?w=450&h=300"
          />
          <span class="span" style="padding-left: 5px">{{ row.name + '.' + row.suffix_name }}</span>
        </div>
      </template>
      <template #file_size="{ row }">
        <span v-if="row.file_size">{{ row.file_size + `KB` }}</span>
      </template>
    </Table>
  </ContentWrap>
</template>
